const mongoose = require('mongoose')
const { Comment, Order } = require('../../schema')
const { ObjectId } = mongoose.Types

mongoose.connect('mongodb://127.0.0.1:27017/sportswear').then()
const commentCollection = mongoose.model('comments', Comment)

const characters = ['jack', 'jodi', 'jai', 'jon', 'jazebelle', 'jed', 'joe', 'jean', 'jake', 'jordan', 'james', 'jia', 'josephine', 'julie', 'jana']

function insertComments ({ proid, userid, imgs, star, content, orderid }) {
    return new Promise(resolve => {
        const record = new commentCollection({
            commentid: ObjectId(),
            proid,
            userid,
            imgs,
            star,
            content,
            time: new Date(new Date().getTime() + 8 * 60 * 60 * 1000)
        })
        record.save().then(res => {
            const orderCollection = mongoose.model('order', Order)
            orderCollection.updateOne({ orderid }, { comment: true }).then(res => {
                resolve('商品评价成功')
            })
        }).catch(err => {
            resolve('失败,id不合法')
        })
    })
}

// 获取全部商品的评价列表
function getCommentList () {
    return new Promise(resolve => {
        commentCollection.aggregate([{
            $lookup: {
                from: 'users',
                foreignField: 'userid',
                localField: 'userid',
                as: 'userInfo'
            }
        }]).sort({ time: -1 }).then(res => {
            resolve(format(res))
        })
    })
}

// 获取指定商品的评价列表
function getCommenListByProid (proid) {
    return new Promise(resolve => {
        try {
            commentCollection.aggregate([{
                $lookup: {
                    from: 'users',
                    foreignField: 'userid',
                    localField: 'userid',
                    as: 'userInfo'
                }
            }]).match({
                proid: ObjectId(proid)
            }).sort({ time: -1 }).then(res => {
                resolve(format(res))
            })
        }catch (e){
            resolve('id不合法')
        }
    })
}

//获取我的商品评价
function getCommenListByUserid (userid) {
    return new Promise(resolve => {
        try {
            commentCollection.aggregate([{
                $lookup: {
                    from: 'users',
                    foreignField: 'userid',
                    localField: 'userid',
                    as: 'userInfo'
                }
            }]).match({
                userid: ObjectId(userid)
            }).sort({ time: -1 }).then(res => {
                resolve(format(res))
            })
        }catch (e){
            resolve('id不合法')
        }
    })
}

// 商家反馈
function returnBack (commentid, msg) {
    return new Promise(resolve => {
        commentCollection.updateOne({ commentid }, {
            msg
        }).then(res => {
            if (res.modifiedCount) {
                resolve('评论成功')
            } else {
                resolve('评论未更新')
            }
        })
    })
}

function format (data) {
    return data.map(item => {
        return {
            _id: item._id,
            commentid: item.commentid,
            proid: item.proid,
            username: item.userInfo[0] && item.userInfo[0].username || '普通用户',
            avatar: `https://joeschmoe.io/api/v1/${characters[Math.floor(Math.random() * 15)]}`,
            content: item.content,
            msg: item.msg,
            imgs: item.imgs,
            star: item.star,
            time: item.time
        }
    })
}

module.exports = {
    insertComments,
    getCommentList,
    getCommenListByProid,
    getCommenListByUserid,
    returnBack
}
